<?php

/**
 * 评论模型
 */
if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class Comment_model extends MY_Model
{

    public $_table = array(
        'comment' => 'comment',
        'user' => 'user',
    );
    public $_fields = array(
        'comment' => array(
            'id' => 'id',
            'postid' => 'postid',
            'userid' => 'userid',
            'addtime' => 'addtime',
            'pid' => 'pid',
            'state' => 'state',
        ),
        'user' => array(
            'id' => 'id',
            'name' => 'name',
        ),
    );

    /**
     * 根据信息ID获取相应的评论数据
     * @param int $postId 信息ID
     */
    public function getByPostId( $postId, $limit= 5, $offset = 0 )
    {
        return $this->db->select( 'c.id,c.postid,c.userid,c.addtime,c.pid,c.content,u.name as username,c.isapply' )
                        ->from( "{$this->_table['comment']} as c" )
                        ->join( "{$this->_table['user']} as u", 'u.id=c.userid' )
                        ->where( 'c.state', 1 )
                        ->order_by( 'c.addtime', 'desc' )
                        ->limit( $limit, $offset )
                        ->get()
                        ->result_array();
    }
    
    /**
     * 根据信息ID获取相应的评论数据
     * @param int $commentId 信息ID
     */
    public function getById( $commentId )
    {
        return $this->db->select( 'c.id,c.postid,c.userid,c.addtime,c.pid,c.content,u.name as username' )
                        ->from( "{$this->_table['comment']} as c" )
                        ->join( "{$this->_table['user']} as u", 'u.id=c.userid' )
                        ->where( 'c.state', 1 )
                        ->where('c.id', $commentId)
                        ->get()
                        ->row_array();
    }

    /**
     * 根据信息ID获取相应的评论数
     * @param int $postId 信息ID
	 * @return int 评论数
     */
	public function getNumByPostId( $postId )
	{
		return $this->db->where( 'postid', $postId )
						->where( 'state', 1 )
						->where( 'isapply', 0 )
                        ->count_all_results($this->_table['comment']);
	}
	
    /**
     * 使用评论ID为主键格式化评论数据
     * @param array $commentData 评论数据
     */
    public function formatByCid( $commentData )
    {
        $result = array();
        foreach( $commentData as $single ) $result[$single['id']] = $single;
        return $result;
    }
    
    /**
     * 插入评论数据
     * @param array $data 评论数据
     * @return int 评论ID
     */
    public function add( $data )
    {
        $insertData = array(
            'postid' => $data['postid'],
            'userid' => $data['userid'],
            'addtime' => time(),
            'pid' => isset( $data['pid'] ) ? $data['pid'] : 0,
            'state' => isset( $data['state'] ) ? $data['state'] : 1,
            'content' => $data['content'],
        );
        $this->db->insert($this->_table['comment'], $insertData);
        return $this->db->insert_id();
    }
    
    public function reply( $data )
    {
        $insertData = array(
            'postid' => $data['postid'],
            'userid' => $data['userid'],
            'addtime' => time(),
            'pid' => $data['pid'],
            'state' => isset( $data['state'] ) ? $data['state'] : 1,
            'content' => $data['content'],
            'isapply' => 1,
        );
        $this->db->insert($this->_table['comment'], $insertData);
        return $this->db->insert_id();
    }
    
    public function deleteByPostId( $postId )
    {
        $this->db->where('postid', $postId)->delete($this->_table['comment']);
    }
    
}

?>
